package org.cy.evolution.net.http.rx;

import org.cy.evolution.net.http.exception.ApiException;
import org.cy.evolution.net.http.exception.ExceptionHandler;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public abstract class AbstractObserver<T> implements Observer<T>, IObserverListener<T> {

    private Disposable d;

    @Override
    public void onSubscribe(Disposable d) {
        this.d = d;
        onRequest();
    }

    @Override
    public void onNext(T t) {
        onSuccess(t);
    }

    @Override
    public void onError(Throwable e) {
        ApiException aex = ExceptionHandler.handle(e);
        onFailure(aex);
    }

    @Override
    public void onComplete() {
        this.d.dispose();
        onFinished();
    }
}
